package com.cloud.pole.shop.controller.goods.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.common.util.TreeUtil;
import com.cloud.pole.shop.biz.goods.ShopGoodsCategoryBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsCategory;
import com.cloud.pole.shop.model.vo.goods.GoodsCategoryTree;
import com.cloud.pole.shop.service.goods.GoodsCategoryService;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 
* <p>Title: 后台管理产品分类</p>  
* <p>Description: </p>  
* @author hsg  
* @date 2019年2月27日
 */
@Controller
@RequestMapping("/goods/category/admin")
@Slf4j
public class GoodsCategoryAdminController extends BaseController<ShopGoodsCategoryBiz, ShopGoodsCategory> {
    @Autowired
    private ShopGoodsCategoryBiz shopGoodsCategoryBiz;
    @Autowired
    private ShopShopsBiz shopShopsBiz;
    @Autowired
    private GoodsCategoryService categoryService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<ShopGoodsCategory> list(String title) {
        Example example = new Example(ShopGoodsCategory.class);
        if (StringUtils.isNotBlank(title)) {
            example.createCriteria().andLike("title", "%" + title + "%");
        }
        return baseBiz.selectByExample(example);
    }

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public List<GoodsCategoryTree> getTree(String title, Integer shopId) {
        Example example = new Example(ShopGoodsCategory.class);
        Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(title)) {
        	criteria.andLike("title", "%" + title + "%");
        }
        if (shopId != null) {
            criteria.andEqualTo("shopId", shopId);
        }
        return getGoodsCategoryTree(baseBiz.selectByExample(example), 0);
    }

    private List<GoodsCategoryTree> getGoodsCategoryTree(List<ShopGoodsCategory> categorys,int root) {
        List<GoodsCategoryTree> trees = new ArrayList<GoodsCategoryTree>();
        GoodsCategoryTree node = null;
        for (ShopGoodsCategory category : categorys) {
            node = new GoodsCategoryTree();
            BeanUtils.copyProperties(category, node);
            node.setId(category.getCategoryId());
            node.setParentId(category.getSupId());
            trees.add(node);
        }
        return TreeUtil.bulid(trees,root) ;
    }
    
	@ResponseBody
	@RequestMapping("/addGoodsCategory")
    public ApiRestResponse<Boolean> addGoodsCategory(
    		@RequestHeader(name="token", defaultValue="") String token,
    		@RequestBody ShopGoodsCategory category){   	
    	try {
			ParamException.err(StringUtils.isBlank(category.getName()), "name不能为空");   
			ParamException.err(category.getShopId() == null, "shopId不能为空");   
			categoryService.addGoodsCategory(token, category);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	@ResponseBody
	@GetMapping("getTopCategory")//查询一级分类
	public List<ShopGoodsCategory> getTopCategory(){
		return categoryService.selectByTop();
	}
	
	@ResponseBody
	@GetMapping("getSecondLevel")//根据一级分类查询二级分类
	public List<ShopGoodsCategory> getSecondLevel(@RequestParam("supId")String supId){
		return categoryService.sleectByCode(supId);
	}
	
}
